package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.MobilePageBO;
import com.slipper.system.codeless.dao.MobilePageDao;
import com.slipper.util.BeanUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.MobileDao;
import com.slipper.system.codeless.bo.MobileBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class MobileService {
    @Resource
    private MobileDao mobileDao;
    @Resource
    private MobilePageDao mobilePageDao;
    
    public MobileBO queryBy(String id) {
        return mobileDao.queryBy(id);
    }
    
    public List<MobileBO> queryList(MobileBO bo) {
        return mobileDao.queryList(bo);
    }
    
    public PageInfo<MobileBO> queryPage(MobileBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(mobileDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(MobileBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return mobileDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());

            MobilePageBO page = new MobilePageBO();
            page.setId(Tool.getUUID());
            page.setParentId(Constants.ROOT_ID);
            page.setMobileId(bo.getId());
            page.setType(MobilePageBO.TYPE_PAGE);
            page.setName("新增页面");
            page.setTitle("新增页面");
            page.setOrdinal(1);
            mobilePageDao.insert(page);

            return mobileDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(MobileBO bo) {
        if (bo.getId() != null) {
            mobilePageDao.deleteByMobileId(bo.getId());
        }
        if (bo.getIds() != null) {
            for (String id : bo.getIds()) {
                mobilePageDao.deleteByMobileId(id);
            }
        }
        return mobileDao.delete(bo);
    }

    @WriteTransactional
    public int updateHome(MobileBO bo) {
        return mobileDao.updateHome(bo);
    }

    @WriteTransactional
    public int updateLogin(MobileBO bo) {
        return mobileDao.updateLogin(bo);
    }

    @WriteTransactional
    public int copy(MobileBO bo) {
        if (Tool.isBlank(bo.getId())) return 0;

        MobileBO source = mobileDao.queryBy(bo.getId());
        MobileBO target = BeanUtil.copy(source, MobileBO.class);
        target.setId(Tool.getUUID());
        target.setName(source.getName() + "_复制");
        target.setUpdateTime(System.currentTimeMillis());
        target.setCreateTime(System.currentTimeMillis());
        mobileDao.insert(target);

        MobilePageBO pageQO = new MobilePageBO();
        pageQO.setMobileId(bo.getId());
        List<MobilePageBO> sourcePgeList = mobilePageDao.queryList(pageQO);
        sourcePgeList.forEach(sourcePage -> {
            MobilePageBO targetPage = BeanUtil.copy(sourcePage, MobilePageBO.class);
            targetPage.setId(Tool.getUUID());
            targetPage.setMobileId(target.getId());
            targetPage.setUpdateTime(System.currentTimeMillis());
            targetPage.setCreateTime(System.currentTimeMillis());
            mobilePageDao.insert(targetPage);
        });

        return 1;
    }

    @WriteTransactional
    public void saveList(List<MobileBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (mobileDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                mobileDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                mobileDao.insert(item);
            }
        });
    }
}
